package com.ruoyi.industry.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.DistanceUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.industry.domain.*;
import com.ruoyi.industry.mapper.*;
import com.ruoyi.industry.service.ISpaceIndustryService;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 产业空间Service业务层处理
 *
 * @author MagicCube
 * @date 2023-05-19
 */
@Service
public class SpaceIndustryServiceImpl implements ISpaceIndustryService {
    @Autowired
    private SpaceIndustryMapper spaceIndustryMapper;

    @Autowired
    private SpaceCollectMapper spaceCollectMapper;

    @Autowired
    private DistrictMapper districtMapper;

    @Autowired
    private ProjectParkMapper projectParkMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private ProjectBuildingMapper projectBuildingMapper;

    @Autowired
    private ProjectWarehouseMapper projectWarehouseMapper;

    @Autowired
    private ProjectWorkshopMapper projectWorkshopMapper;

    @Autowired
    private ProjectApartmentMapper projectApartmentMapper;

    @Autowired
    private ProjectAuctionMapper projectAuctionMapper;

    @Autowired
    private ProjectLandMapper projectLandMapper;

    /**
     * 查询产业空间
     *
     * @param id 产业空间主键
     * @return 产业空间
     */
    @Override
    public SpaceIndustry selectSpaceIndustryById(Long id) {
        return spaceIndustryMapper.selectSpaceIndustryById(id);
    }

    /**
     * 查询产业空间列表
     *
     * @param spaceIndustry 产业空间
     * @return 产业空间
     */
    @Override
    public List<SpaceIndustry> selectSpaceIndustryList(SpaceIndustryVo spaceIndustry) {
        List<SpaceIndustry> spaceIndustries = null;
        if (spaceIndustry.getBuildType() != null && spaceIndustry.getBuildType() == 23) {
            List<Integer> buildTypes = new ArrayList<>();
            buildTypes.add(2);
            buildTypes.add(3);
            spaceIndustry.setBuildTypes(buildTypes);
            spaceIndustry.setBuildType(null);
        }
        spaceIndustries = spaceIndustryMapper.selectIndustryList(spaceIndustry);
        return spaceIndustries;
    }

    @Override
    public List<SpaceIndustry> selectSpaceIndustryListz(SpaceIndustry spaceIndustry, Boolean login) {
        extracted(spaceIndustry);
        List<SpaceIndustry> spaceIndustries = spaceIndustryMapper.selectSpaceIndustryListz(spaceIndustry);
        spaceInfo(spaceIndustries);
        return checkLogin(login, spaceIndustries);
    }

    public void spaceInfo(List<SpaceIndustry> spaceIndustries) {
        for (SpaceIndustry industry : spaceIndustries) {
            SysUser sysUser = new SysUser();
            switch (industry.getBuildType()){
                case 1:
                    ProjectPark projectPark = projectParkMapper.selectProjectParkByNumber(industry.getSpaceId());
                    if (projectPark.getServicePhone()!=null) {
                        sysUser = spaceIndustryMapper.selectSysUserById(projectPark.getServicePhone());
                    }
                    if (sysUser!=null){
                        industry.setContactName(sysUser.getNickName());
                        industry.setContactPhone(sysUser.getPhonenumber());
                        industry.setQrCode(sysUser.getCodePic());
                    }else {
                        industry.setContactName(projectPark.getContactName());
                        industry.setContactPhone(projectPark.getContactPhone());
                        industry.setQrCode(projectPark.getQrCode());
                    }
                    industry.setReserveState(projectPark.getReserveState());
                    industry.setServicePhone(projectPark.getServicePhone());
                    break;
                case 2:
                case 3:
                    ProjectBuilding projectBuilding = projectBuildingMapper.selectProjectBuildingByNumber(industry.getSpaceId());
                    if (projectBuilding.getServicePhone()!=null) {
                        sysUser = spaceIndustryMapper.selectSysUserById(projectBuilding.getContactName());
                    }
                    if (sysUser!=null){
                        industry.setContactName(sysUser.getNickName());
                        industry.setContactPhone(sysUser.getPhonenumber());
                        industry.setQrCode(sysUser.getCodePic());
                    }else {
                        industry.setContactName(projectBuilding.getContactName());
                        industry.setContactPhone(projectBuilding.getContactPhone());
                        industry.setQrCode(projectBuilding.getQrCode());
                    }
                    industry.setReserveState(projectBuilding.getReserveState());
//                    industry.setContactName(projectBuilding.getContactName());
//                    industry.setContactPhone(projectBuilding.getContactPhone());
                    industry.setServicePhone(projectBuilding.getServicePhone());
                    break;
                case 4:
                    ProjectWarehouse projectWarehouse = projectWarehouseMapper.selectProjectWarehouseByNumber(industry.getSpaceId());
                    if (projectWarehouse.getServicePhone()!=null) {
                        sysUser = spaceIndustryMapper.selectSysUserById(projectWarehouse.getContactName());
                    }
                    if (sysUser!=null){
                        industry.setContactName(sysUser.getNickName());
                        industry.setContactPhone(sysUser.getPhonenumber());
                        industry.setQrCode(sysUser.getCodePic());
                    }else {
                        industry.setContactName(projectWarehouse.getContactName());
                        industry.setContactPhone(projectWarehouse.getContactPhone());
                        industry.setQrCode(projectWarehouse.getQrCode());
                    }
                    industry.setReserveState(projectWarehouse.getReserveState());
//                    industry.setContactName(projectWarehouse.getContactName());
//                    industry.setContactPhone(projectWarehouse.getContactPhone());
                    industry.setServicePhone(projectWarehouse.getServicePhone());
                    break;
                case 5:
                    ProjectWorkshop projectWorkshop = projectWorkshopMapper.selectProjectWorkshopByNumber(industry.getSpaceId());
                    if (projectWorkshop.getServicePhone()!=null) {
                        sysUser = spaceIndustryMapper.selectSysUserById(projectWorkshop.getContactName());
                    }
                    if (sysUser!=null){
                        industry.setContactName(sysUser.getNickName());
                        industry.setContactPhone(sysUser.getPhonenumber());
                        industry.setQrCode(sysUser.getCodePic());
                    }else {
                        industry.setContactName(projectWorkshop.getContactName());
                        industry.setContactPhone(projectWorkshop.getContactPhone());
                        industry.setQrCode(projectWorkshop.getQrCode());
                    }
                    industry.setReserveState(projectWorkshop.getReserveState());
//                    industry.setContactName(projectWorkshop.getContactName());
//                    industry.setContactPhone(projectWorkshop.getContactPhone());
                    industry.setServicePhone(projectWorkshop.getServicePhone());
                    break;
                case 6:
                    ProjectApartment projectApartment = projectApartmentMapper.selectProjectApartmentByNumber(industry.getSpaceId());
                    if (projectApartment.getServicePhone()!=null) {
                        sysUser = spaceIndustryMapper.selectSysUserById(projectApartment.getContactName());
                    }
                    if (sysUser!=null){
                        industry.setContactName(sysUser.getNickName());
                        industry.setContactPhone(sysUser.getPhonenumber());
                        industry.setQrCode(sysUser.getCodePic());
                    }else {
                        industry.setContactName(projectApartment.getContactName());
                        industry.setContactPhone(projectApartment.getContactPhone());
                        industry.setQrCode(projectApartment.getQrCode());
                    }
                    industry.setReserveState(projectApartment.getReserveState());
//                    industry.setContactName(projectApartment.getContactName());
//                    industry.setContactPhone(projectApartment.getContactPhone());
                    industry.setServicePhone(projectApartment.getServicePhone());
                    break;
                case 7:
                    ProjectAuction projectAuction = projectAuctionMapper.selectProjectAuctionByNumber(industry.getSpaceId());
                    if (projectAuction.getServicePhone()!=null) {
                        sysUser = spaceIndustryMapper.selectSysUserById(projectAuction.getContactName());
                    }
                    if (sysUser!=null){
                        industry.setContactName(sysUser.getNickName());
                        industry.setContactPhone(sysUser.getPhonenumber());
                        industry.setQrCode(sysUser.getCodePic());
                    }else {
                        industry.setContactName(projectAuction.getContactName());
                        industry.setContactPhone(projectAuction.getContactPhone());
                        industry.setQrCode(projectAuction.getQrCode());
                    }
                    industry.setReserveState(projectAuction.getReserveState());
                    industry.setSellPrice(projectAuction.getAuctionPriceLower()+"-"+projectAuction.getAuctionPriceUpper());
                    industry.setSellPriceLow(projectAuction.getAuctionPriceLower());
//                    industry.setContactName(projectAuction.getContactName());
//                    industry.setContactPhone(projectAuction.getContactPhone());
                    industry.setSellPriceHigh(projectAuction.getAuctionPriceUpper());
                    industry.setServicePhone(projectAuction.getServicePhone());
                    break;
                case 8:
                    ProjectLand projectLand = projectLandMapper.selectProjectLandByNumber(industry.getSpaceId());
                    if (projectLand.getServicePhone()!=null) {
                        sysUser = spaceIndustryMapper.selectSysUserById(projectLand.getContactName());
                    }
                    if (sysUser!=null){
                        industry.setContactName(sysUser.getNickName());
                        industry.setContactPhone(sysUser.getPhonenumber());
                        industry.setQrCode(sysUser.getCodePic());
                    }else {
                        industry.setContactName(projectLand.getContactName());
                        industry.setContactPhone(projectLand.getContactPhone());
                        industry.setQrCode(projectLand.getQrCode());
                    }
                    industry.setReserveState(projectLand.getReserveState());
//                    industry.setContactName(projectLand.getContactName());
//                    industry.setContactPhone(projectLand.getContactPhone());
                    industry.setServicePhone(projectLand.getServicePhone());
                    break;
            }
        }
    }

    @Override
    public List<SpaceIndustry> selectHighGradeList(SpaceIndustry spaceIndustry) {
        extracted(spaceIndustry);
        List<SpaceIndustry> spaceIndustries = spaceIndustryMapper.selectHighGradeList(spaceIndustry);
        spaceInfo(spaceIndustries);
        return spaceIndustries;
    }
    private void extracted(SpaceIndustry spaceIndustry) {
        //出售价格
        if (spaceIndustry.getSellPrice() != null && !spaceIndustry.getSellPrice().equals("")) {
            String[] split = spaceIndustry.getSellPrice().split("~");
            spaceIndustry.setSellPriceLow(Long.parseLong(split[0]));
            if (!split[1].equals("N")) {
                spaceIndustry.setSellPriceHigh(Long.parseLong(split[1]));
            }
        }
        //租赁价格
        if (spaceIndustry.getLeasePrice() != null && !spaceIndustry.getLeasePrice().equals("")) {
            String[] split = spaceIndustry.getLeasePrice().split("~");
            spaceIndustry.setLeasePriceLow(Long.parseLong(split[0]));
            if (!split[1].equals("N")) {
                spaceIndustry.setLeasePriceHigh(Long.parseLong(split[1]));
            }
        }
    }
    @NotNull
    public List<SpaceIndustry> checkLogin(Boolean login, List<SpaceIndustry> spaceIndustries) {
        if (login) {
            for (SpaceIndustry industry : spaceIndustries) {
                if (industry.getParkName() == null) {
                    industry.setParkName("");
                }
                if (industry.getSpaceName() == null) {
                    industry.setSpaceName("");
                }
            }
            checkCollection(spaceIndustries);
            return spaceIndustries;
        } else {
            for (SpaceIndustry industry : spaceIndustries) {
                if (industry.getParkName() != null && !industry.getParkName().equals("")) {
                    industry.setParkName(industry.getParkName().substring(0, 1) + "******");
                } else {
                    industry.setParkName("");
                }
                if (industry.getSpaceName() != null && !industry.getSpaceName().equals("")) {
                    industry.setSpaceName(industry.getSpaceName().substring(0, 1) + "******");
                } else {
                    industry.setSpaceName("*");
                }
            }
            return spaceIndustries;
        }
    }
    private void checkCollection(List<SpaceIndustry> spaceIndustries) {
        //查询所有收藏的空间编码
        List<String> list = spaceCollectMapper.selectSpaceCollect(SecurityUtils.getUserId());
        //循环判断是否有收藏
        if (list != null && list.size() != 0) {
            for (SpaceIndustry industry : spaceIndustries) {
                if (list.contains(industry.getSpaceId())) {
                    industry.setCollectState(1);
                }
            }
        }
    }
    /**
     * 新增产业空间
     *
     * @param spaceIndustry 产业空间
     * @return 结果
     */
    @Override
    public int insertSpaceIndustry(SpaceIndustry spaceIndustry) {
        spaceIndustry.setCreateTime(DateUtils.getNowDate());
        return spaceIndustryMapper.insertSpaceIndustry(spaceIndustry);
    }

    /**
     * 修改产业空间
     *
     * @param spaceIndustry 产业空间
     * @return 结果
     */
    @Override
    public int updateSpaceIndustry(SpaceIndustry spaceIndustry) {
        spaceIndustry.setUpdateTime(DateUtils.getNowDate());
        return spaceIndustryMapper.updateSpaceIndustry(spaceIndustry);
    }

    /**
     * 批量删除产业空间
     *
     * @param ids 需要删除的产业空间主键
     * @return 结果
     */
    @Override
    public int deleteSpaceIndustryByIds(Long[] ids) {
        return spaceIndustryMapper.deleteSpaceIndustryByIds(ids);
    }

    /**
     * 删除产业空间信息
     *
     * @param id 产业空间主键
     * @return 结果
     */
    @Override
    public int deleteSpaceIndustryById(Long id) {
        return spaceIndustryMapper.deleteSpaceIndustryById(id);
    }

    @Override
    public Map<String, Object> statistics() {
        List<SpaceIndustry> spaceIndustries = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        if (SecurityUtils.getLoginUser().getRoles().contains("admin")) {
            spaceIndustries = spaceIndustryMapper.selectSpaceIndustryList(null);
        } else {
            SpaceIndustry spaceIndustry = new SpaceIndustry();
            spaceIndustry.setSupplyUserId(SecurityUtils.getUserId());
            spaceIndustries = spaceIndustryMapper.selectSpaceIndustryList(spaceIndustry);
        }
        Map<Integer, List<SpaceIndustry>> collect = spaceIndustries.stream().collect(Collectors.groupingBy(SpaceIndustry::getBuildType));
        String monthStart = DateUtils.getMonthStart();
        Date month = DateUtils.parseDate(monthStart);
        String weekStart = DateUtils.getWeekStart();
        Date week = DateUtils.parseDate(weekStart);
        map.put("sum", spaceIndustries.size());
        map.put("month", spaceIndustries.stream().filter(bussEnvIntro -> bussEnvIntro.getCreateTime().compareTo(month) >= 0).count());
        map.put("week", spaceIndustries.stream().filter(bussEnvIntro -> bussEnvIntro.getCreateTime().compareTo(week) >= 0).count());
        Map<Integer, List<SpaceIndustry>> monthMap = spaceIndustries.stream().collect(Collectors.groupingBy(o -> {
            Calendar c = Calendar.getInstance();
            c.setTime(o.getCreateTime());/*创建时间*/
            return c.get(Calendar.MONTH) + 1;
        }));
        Map<Integer, Integer> map1 = new HashMap<>();
        for (int i = 1; i < 13; i++) {
            if (monthMap.get(i) != null) {
                map1.put(i, monthMap.get(i).size());
            } else {
                map1.put(i, 0);
            }
        }
        map.put("monthly", map1);
        for (int i = 1; i <= 8; i++) {
            List<SpaceIndustry> entry = collect.get(i);
            Map<String, Object> space = new HashMap<>();
            space.put("type", i);
            if (entry != null && entry.size()!=0) {
                space.put("sum", entry.size());
                space.put("month", entry.stream().filter(bussEnvIntro -> bussEnvIntro.getCreateTime().compareTo(month) >= 0).count());
                space.put("week", entry.stream().filter(bussEnvIntro -> bussEnvIntro.getCreateTime().compareTo(week) >= 0).count());
                BigDecimal bigDecimal = buildBigDecimal(entry.size(), spaceIndustries.size());
                space.put("compared", bigDecimal + "%");
            } else {
                entry = new ArrayList<>();
                space.put("sum", 0);
                space.put("month", 0);
                space.put("week", 0);
                space.put("compared", "0%");
            }
            switch (i) {
                case 1:
                    Map<Integer, List<SpaceIndustry>> listMap = entry.stream().collect(Collectors.groupingBy(o -> {
                        Calendar c = Calendar.getInstance();
                        c.setTime(o.getCreateTime());/*创建时间*/
                        return c.get(Calendar.MONTH) + 1;
                    }));
                    Map<Integer, Integer> map2 = new HashMap<>();
                    for (int j = 1; j < 13; j++) {
                        if (listMap.get(j) != null) {
                            map2.put(j, listMap.get(j).size());
                        } else {
                            map2.put(j, 0);
                        }
                    }
                    space.put("monthly", map2);
                    map.put("park", space);
                    break;
                case 2:
                    map.put("premises", space);
                    break;
                case 3:
                    map.put("building", space);
                    break;
                case 4:
                    map.put("warehouse", space);
                    break;
                case 5:
                    map.put("workshop", space);
                    break;
                case 6:
                    map.put("apartment", space);
                    break;
                case 7:
                    map.put("auction", space);
                    break;
                case 8:
                    map.put("land", space);
                    break;
            }
        }

        return map;
    }


    public static void deleteFile(List<String> pathList, String vrResource, String imgResource, String coverResource, String vrcover, String qrCode) {
        if (vrResource != null && !vrResource.equals("")) {
            pathList.addAll(Arrays.asList(vrResource.split(",")));//VR资源
        }
        if (imgResource != null && !imgResource.equals("")) {
            pathList.addAll(Arrays.asList(imgResource.split(",")));//图片资源
        }
        if (coverResource != null && coverResource.equals("")) {
            pathList.add(coverResource);//封面资源
        }
        if (vrcover != null && vrcover.equals("")) {
            pathList.add(vrcover);//VR封面
        }
        if (qrCode != null && qrCode.equals("")) {
            pathList.add(qrCode);//微信二维码
        }
    }

    /*生成检测报告同比*/
    public BigDecimal buildBigDecimal(int a, int b) {

        if (a != 0 && b != 0) {
            BigDecimal now = new BigDecimal(a);
            BigDecimal old = new BigDecimal(b);
            BigDecimal divide = now.divide(old, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
            return divide.setScale(2, RoundingMode.HALF_UP);
        } else if (a != 0) {
            return new BigDecimal("100.00");
        } else if (b != 0) {
            return new BigDecimal("-100.00");
        } else {
            return new BigDecimal("0.00");
        }
    }
    /**
     * 人气查询
     *
     * @param spaceIndustry 产业信息
     * @return 结果
     */
    @Override
    public List<SpaceIndustry> selectSpaceIndustryPopularList(SpaceIndustry spaceIndustry, Boolean login) {
        extracted(spaceIndustry);
        List<SpaceIndustry> spaceIndustries = spaceIndustryMapper.selectSpaceIndustryPopularList(spaceIndustry);
        return checkLogin(login, spaceIndustries);
    }
    /**
     * 最新上架
     *
     * @param spaceIndustry 产业信息
     * @return 结果
     */
    @Override
    public List<SpaceIndustry> selectSpaceIndustryNewlyAddedList(SpaceIndustry spaceIndustry, Boolean login) {
        extracted(spaceIndustry);
        List<SpaceIndustry> spaceIndustries = spaceIndustryMapper.selectSpaceIndustryNewlyAddedList(spaceIndustry);
        return checkLogin(login, spaceIndustries);
    }

    @Override
    public List<SpaceIndustry> selectSpaceCheckIssueList(SpaceIndustry spaceIndustry) {
        extracted(spaceIndustry);
        spaceIndustry.setSupplyUserId(SecurityUtils.getUserId());
        List<SpaceIndustry> spaceIndustries = spaceIndustryMapper.selectSpaceCheckIssueList(spaceIndustry);
        checkCollection(spaceIndustries);
        return spaceIndustries;
    }

    @Override
    public void updatePageViews(String spaceId) {
        spaceIndustryMapper.updatePageViews(spaceId);
    }

    @Override
    public List<SpaceIndustry> selectNearbySpace(SpaceIndustry spaceIndustry) {
        List<Long> cityIds = new ArrayList<>();
        if (spaceIndustry.getCityId() != null) {
            cityIds.add(spaceIndustry.getCityId());
        } else if (spaceIndustry.getProvinceId() != null) {
            cityIds.add(spaceIndustry.getProvinceId());
            cityIds.addAll(districtMapper.selectDistrictIdList(spaceIndustry.getProvinceId()));
        }
        if (cityIds.size() != 0) {
            spaceIndustry.setCityIds(cityIds);
        }
        List<SpaceIndustry> industryList = spaceIndustryMapper.selectSpaceIndustryListx(spaceIndustry);
        List<SpaceIndustry> spaceIndustries = industryList.stream().filter(SpaceIndustry -> (!SpaceIndustry.getSpaceId().equals(spaceIndustry.getSpaceId()))).collect(Collectors.toList());
        if (spaceIndustry.getLongitude() != null && spaceIndustry.getLatitude() != null) {
            for (SpaceIndustry industry : spaceIndustries) {
                if (industry.getLongitude() != null && industry.getLatitude() != null) {
                    double distance = DistanceUtils.Distance(spaceIndustry.getLongitude(), spaceIndustry.getLatitude(), industry.getLongitude(), industry.getLatitude());
                    industry.setDistance(distance);
                }
            }
        }
        List<SpaceIndustry> collect = spaceIndustries.stream()
                .filter(SpaceIndustry -> (SpaceIndustry.getDistance() != null))
                .sorted(Comparator.comparing(SpaceIndustry::getDistance))
                .limit(5)
                .collect(Collectors.toList());
        if (collect.size() < 5) {
            List<SpaceIndustry> collect1 = spaceIndustries.stream()
                    .filter(SpaceIndustry -> (SpaceIndustry.getDistance() == null))
                    .sorted(Comparator.comparing(SpaceIndustry::getCreateTime))
                    .collect(Collectors.toList());
            for (int i = 0; i < 5 - collect.size() && i < collect1.size(); i++) {
                collect.add(collect1.get(i));
            }
        }
        return collect;
    }

    @Override
    public Map<String,Object> dataComparison() {
        Map<String, Object> map = new HashMap<>();
        /*经济带统计*/
        List<Object> economicBelt = new ArrayList<>();
        //查询经济带列表
        List<DictDataVo> sysDictData = spaceIndustryMapper.selectEconomicBeltList();
        for (DictDataVo dictData : sysDictData) {
            Long count = 0L;
            Map<String, Object> statistics = new HashMap<>();
            //根据经济带查询各经济带园区列表
            ProjectPark projectPark = new ProjectPark();
            projectPark.setParkEconomic(dictData.getDictValue());
            List<ProjectPark> projectParks = projectParkMapper.selectProjectParkList(projectPark);
            //根据园区id查询各产业物理空间数据
            for (ProjectPark park : projectParks) {
                //统计各部分的数量
                SpaceIndustry spaceIndustry = new SpaceIndustry();
                spaceIndustry.setParkId(String.valueOf(park.getId()));
                List<SpaceIndustry> spaceIndustries = spaceIndustryMapper.selectSpaceIndustryList(spaceIndustry);
                count = count + spaceIndustries.size();
            }
            statistics.put("type",dictData.getDictLabel());
            statistics.put("count",count);
            economicBelt.add(statistics);
        }
        map.put("economicBelt",economicBelt);
        /*空间类型统计*/
        Map<String, Object> spaceType = new HashMap<>();
        //查询所有产业物理空间数据
        List<SpaceIndustry> spaceIndustries = spaceIndustryMapper.selectSpaceIndustryList(null);
        Map<Integer, List<SpaceIndustry>> collect = spaceIndustries.stream().collect(Collectors.groupingBy(SpaceIndustry::getBuildType));
        for (int i = 1; i <= 8; i++) {
            List<SpaceIndustry> entry = collect.get(i);
            Map<String, Object> space = new HashMap<>();
            space.put("type", i);
            if (entry != null && entry.size()!=0) {
                BigDecimal bigDecimal = buildBigDecimal(entry.size(), spaceIndustries.size());
                space.put("count",entry.size());
                space.put("compared", bigDecimal + "%");
            } else {
                space.put("compared", "0%");
            }
            switch (i){
                case 1:
                    spaceType.put("park",space);
                    break;
                case 2:
                    spaceType.put("premises", space);
                    break;
                case 3:
                    spaceType.put("building", space);
                    break;
                case 4:
                    spaceType.put("warehouse", space);
                    break;
                case 5:
                    spaceType.put("workshop", space);
                    break;
                case 6:
                    spaceType.put("apartment", space);
                    break;
                case 7:
                    spaceType.put("auction", space);
                    break;
                case 8:
                    spaceType.put("land", space);
                    break;
            }
        }
        map.put("spaceType",spaceType);
        return map;
    }

    @Override
    public Map<String, Object> dataOverview() {
        /*产业物理空间已发布数量*/
        Map<String, Object> map = new HashMap<>();
        //查询全部产业物理空间数据
        List<SpaceIndustry> spaceIndustries = new ArrayList<>();
        if (SecurityUtils.getLoginUser().getRoles().contains("admin")) {
            spaceIndustries = spaceIndustryMapper.selectSpaceIndustryList(null);
        } else {
            SpaceIndustry spaceIndustry = new SpaceIndustry();
            spaceIndustry.setSupplyUserId(SecurityUtils.getUserId());
            spaceIndustries = spaceIndustryMapper.selectSpaceIndustryList(spaceIndustry);
        }
        //根据年月日统计
        String yearStart = DateUtils.getYearStart();
        Date year = DateUtils.parseDate(yearStart);
        String monthStart = DateUtils.getMonthStart();
        Date month = DateUtils.parseDate(monthStart);
        String weekStart = DateUtils.getWeekStart();
        Date week = DateUtils.parseDate(weekStart);
        map.put("sum",spaceIndustries.size());
        map.put("year",spaceIndustries.stream().filter(bussEnvIntro -> bussEnvIntro.getCreateTime().compareTo(year) >= 0).count());
        map.put("moth",spaceIndustries.stream().filter(bussEnvIntro -> bussEnvIntro.getCreateTime().compareTo(month) >= 0).count());
        map.put("week",spaceIndustries.stream().filter(bussEnvIntro -> bussEnvIntro.getCreateTime().compareTo(week) >= 0).count());
        return map;
    }

    private final static String ONLINE_INFO = "onlineInfo:";

    @Override
    public Map<String, Object> getUserClickRate() {
        Map<String, Object> map = new HashMap<>();
        /*查询用户发布的信息*/
        if (SecurityUtils.getLoginUser().getRoles().contains("government")) {
            //查询区域营商环境在线咨询点击数量
            List<String> list = spaceIndustryMapper.selectBusinessIdsByUserId(SecurityUtils.getUserId());
            long count = 0L;
            for (String businessId : list) {
                Integer cacheMapValue = redisService.getCacheMapValue(ONLINE_INFO + "business", businessId);
                if (cacheMapValue!=null) {
                    count = count + cacheMapValue;
                }
            }
            map.put("business",count);
        }

        //查询产业物理空间在线咨询点击量
        List<SpaceIndustry> list = spaceIndustryMapper.selectSpaceIndustryIdByUserId(SecurityUtils.getUserId());
        long count = 0L;
        for (SpaceIndustry space : list) {
            Integer cacheMapValue = redisService.getCacheMapValue(ONLINE_INFO + "industry", space.getSpaceId());
            if (cacheMapValue!=null) {
                count = count + cacheMapValue;
            }
        }
        map.put("industry",count);
        return map;
    }

    @Override
    public Map<String, List<SpaceIndustry>> getNumberOfClicks() {
        /*产业物理空间*/
        //查询该用户产业物理空间发布信息
        List<SpaceIndustry> spaceIndustries = spaceIndustryMapper.selectSpaceIndustryIdByUserId(SecurityUtils.getUserId());
        //分类并返回
        Map<Integer, List<SpaceIndustry>> collect = spaceIndustries.stream().collect(Collectors.groupingBy(SpaceIndustry::getBuildType));
        Map<String, List<SpaceIndustry>> map = new HashMap<>();
        for (Map.Entry<Integer, List<SpaceIndustry>> entry : collect.entrySet()) {
            List<SpaceIndustry> list = entry.getValue().stream().sorted(Comparator.comparing(SpaceIndustry::getPageViews).reversed()).collect(Collectors.toList());
            switch (entry.getKey()){
                case 1:
                    map.put("park",list);
                    break;
                case 2:
                    map.put("premises", list);
                    break;
                case 3:
                    map.put("building", list);
                    break;
                case 4:
                    map.put("warehouse", list);
                    break;
                case 5:
                    map.put("workshop", list);
                    break;
                case 6:
                    map.put("apartment", list);
                    break;
                case 7:
                    map.put("auction", list);
                    break;
                case 8:
                    map.put("land", list);
                    break;
            }
        }
        return map;
    }
}
